package se.webbzon.oschi01.worldobject.appearance;

import javax.media.j3d.Link;
import javax.media.j3d.Switch;

import se.webbzon.oschi01.modelset.Model3D;
import se.webbzon.oschi01.resourceset.ResourceSet;

public class SimpleModelAppearance3D extends AbstractAppearance3D {
	
	// A switch used to turn visibility on and off
	private final Switch switchGroup;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public SimpleModelAppearance3D(int renderingOrder, ResourceSet<Model3D> modelSet) {
		super(renderingOrder);
		final Link linkGroup = new Link(modelSet.get(0).getSharedGroup());
		
		switchGroup = new Switch();
		switchGroup.setCapability(Switch.ALLOW_SWITCH_READ);
		switchGroup.setCapability(Switch.ALLOW_SWITCH_WRITE);
		switchGroup.setWhichChild(Switch.CHILD_ALL);
		switchGroup.addChild(linkGroup);
		
		getTransformGroup().addChild(switchGroup);
	}
	
	/*============================================================
	Public methods
	============================================================*/

	@Override public boolean getVisible() {
		return ((switchGroup.getWhichChild() & Switch.CHILD_ALL) > 0);
	}

	@Override public void setVisible(boolean visible) {
		if (visible)
			switchGroup.setWhichChild(Switch.CHILD_ALL);
		else
			switchGroup.setWhichChild(Switch.CHILD_NONE);
	}

}
